package cn.hnu.qq.client.controller;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.InetSocketAddress;

import cn.hnu.qq.entity.User;
import cn.hnu.qq.entity.message.ChartMessage;

public class DefaultModelChartController extends CenterController {
	
	private static DefaultModelChartController instance = null;
	
	private DefaultModelChartController(){};
	
	public static synchronized DefaultModelChartController getInstance(){
		if(instance == null){
			instance = new DefaultModelChartController();
		}
		return instance;
	}

	public void sendChartMessage(User ownner, String info) {
		ChartMessage message = new ChartMessage();
		message.setMes(info);
		message.setAccount(getCurUser().getAcount());
		byte[] datas = ObjectToArray(message);
		
		InetSocketAddress isa;
		try {
			isa = new InetSocketAddress(InetAddress.getByName(ownner.getIp()), ownner.getUserClientPort());
			DatagramPacket dp = new DatagramPacket(datas,datas.length,isa);
			getdSocket().send(dp);
			log.debug("通过udp发送消息,发送至端口："+ownner.getIp());
		} catch (Exception e) {
			System.err.println("DefaultModelChartController sendChartMessage");
			log.error("DefaultModelChartController sendChartMessage");
			e.printStackTrace();
		}
		
		
		
		
	}

	public void receiveChartMessage(ChartMessage message) {
//		message.getAccount()
		
	}
}
